package cn.oop.plane;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Date;

public class MainGame extends GameFrame {
    Image bj = GameUtil.getImage("image/bj.jpg");

    MyPlane p = new MyPlane("image/palne.png", 50, 50);

    ArrayList bulletList = new ArrayList();

    Date startTime;
    Date endTime;
    Date nowTime;

    public void paint(Graphics g) {
        g.drawImage(bj, 0, 0, null);
        p.draw(g);

        for (int i = 0; i < bulletList.size(); i++) {
            Bullet b = (Bullet) bulletList.get(i);
            b.draw(g);

            if (p.isLive()) {
                nowTime = new Date();
                long period1 = (int) ((nowTime.getTime() - startTime.getTime()) / 1000);
                printInfo(g, "游戏已进行时间为:" + period1 + "秒", 30, 600, 60,
                        Color.red);
                switch ((int) (period1 / 10)) {
                    case 1:
                        b.speed += 0.1;
                        break;
                    case 2:
                        b.speed += 0.2;
                    case 3:
                        b.speed += 0.3;
                    default:
                        b.speed += 0.4;
                        break;
                }
                // 检测跟飞机的碰撞
                boolean peng = b.getRect().intersects(p.getRect());

                if (peng) {
                    p.peng("image/baozha.jpg");
                    p.setLive(false); // 飞机死掉!
                    endTime = new Date();
                }
            } else {

                int period = (int) (endTime.getTime() - startTime.getTime()) / 1000;
                printInfo(g, "时间:" + period + "秒", 30, 150, 260, Color.white);
                switch ((int) (period / 10)) {
                    case 0:
                    case 1:
                        printInfo(g, "菜鸟", 50, 100, 200, Color.white);
                        break;
                    case 2:
                        printInfo(g, "小鸟", 50, 100, 200, Color.white);
                        break;
                    case 3:
                        printInfo(g, "大鸟", 50, 100, 200, Color.yellow);
                        break;
                    case 4:
                        printInfo(g, "鸟王子", 50, 100, 200, Color.red);
                        break;
                    default:
                        printInfo(g, "鸟人", 50, 100, 200, Color.pink);
                        break;

                }
            }
        }

        // printInfo(g, "分数:100",10,60,70,Color.white);
    }

    // 在窗口上打印信息

    public void printInfo(Graphics g, String str, int size, int x, int y,
                          Color color) {
        Color c = g.getColor();
        g.setColor(color);
        Font f = new Font("宋体", Font.BOLD, size);
        g.setFont(f);
        g.drawString(str, x, y);
        g.setColor(c);
    }

    // 双缓冲效果
    private Image offScreenImage = null;

    public void update(Graphics g) {
        if (offScreenImage == null)
            offScreenImage = this.createImage(Constant.GAME_WIDTH,
                    Constant.GAME_HEIGHT);

        Graphics gOff = offScreenImage.getGraphics();
        paint(gOff);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    // 主程序
    public static void main(String[] args) {
        new MainGame().launchFrame();

    }

    public void launchFrame() {
        super.launchFrame();
        // 增加键盘的监听
        addKeyListener(new KeyMonitor());

        // 生成一堆子弹
        for (int i = 0; i < 100; i++) {
            Bullet b = new Bullet();
            bulletList.add(b);
        }
        startTime = new Date();
    }

    // 定义为内部类,可以方便的使用外部类的普通属性
    class KeyMonitor extends KeyAdapter {

        public void keyPressed(KeyEvent e) {
            p.reduceDirection(e);
        }

        public void keyReleased(KeyEvent e) {
            p.addDirection(e);
        }
    }
}
